package cn.chuanw.db.service;

import cn.chuanw.db.dao.WHousepayRecordMapper;
import cn.chuanw.db.domain.WHousepayRecordExample;
import com.github.pagehelper.PageHelper;
import cn.chuanw.db.domain.WHousepayRecord;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class WHousepayRecordService {
    @Resource
    private WHousepayRecordMapper wHousepayRecordMapper;
   
    public List<WHousepayRecord> querySelective(String houseId,String costType,Integer page, Integer size) {
    	WHousepayRecordExample wHousepayRecordExample = new WHousepayRecordExample();
    	WHousepayRecordExample.Criteria criteria = wHousepayRecordExample.createCriteria();
        if (!StringUtils.isEmpty(houseId)) {
            criteria.andHouseIdLike("%" +houseId+"%" );
        }
        if (!StringUtils.isEmpty(costType)) {
            criteria.andCostTypeEqualTo(Integer.parseInt(costType));
        }
        criteria.andDelFlagEqualTo(0);
        PageHelper.startPage(page, size);
        return wHousepayRecordMapper.selectByExample(wHousepayRecordExample);
    }

    public void addWHouseInfo(WHousepayRecord wHousepayRecord) {
    	wHousepayRecordMapper.insertSelective(wHousepayRecord);
    }

    public int updateById(WHousepayRecord wHousepayRecord) {
        return wHousepayRecordMapper.updateByPrimaryKeySelective(wHousepayRecord);
    }

    public void archiveIncomeRent(String ids) {
        if (!StringUtils.isEmpty(ids)) {
            String[] split = ids.split(",");
            for (String s : split) {
            	WHousepayRecord wHousepayRecord = new WHousepayRecord();
            	WHousepayRecord wHousepayRecords = wHousepayRecordMapper.selectByPrimaryKey(s);
                wHousepayRecord.setId(s);
                wHousepayRecord.setCostType(wHousepayRecords.getCostType()+10);              
                wHousepayRecordMapper.updateByPrimaryKeySelective(wHousepayRecord);
            }
        }
    }


    /**
     * @Auther: wangkun
     * @Date:  20-03-24 上午 9:46
     * @Description: 计算当前收租金额，归档收租金额
     **/
    public Object tatolIncomeRent(){
        Integer electric = wHousepayRecordMapper.sumMoney(0);//当前电费
        Integer water = wHousepayRecordMapper.sumMoney(1);//当前水费
        Integer gas = wHousepayRecordMapper.sumMoney(2);//当前燃气
        Integer archiveElectric = wHousepayRecordMapper.sumMoney(10); //归档电费
        Integer archiveWater = wHousepayRecordMapper.sumMoney(11); //归档水费
        Integer archiveGas = wHousepayRecordMapper.sumMoney(12); //归档燃气费
        Map<String, Object> data = new HashMap<String, Object>();
        data.put("electric",electric==null?0:electric);
        data.put("water",water==null?0:water);
        data.put("gas",gas==null?0:gas);
        data.put("archiveElectric",archiveElectric==null?0:archiveElectric);
        data.put("archiveWater",archiveWater==null?0:archiveWater);
        data.put("archiveGas",archiveGas==null?0:archiveGas);
        return data;
    }
}
